home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 22 / CU Amiga Magazine's Super CD-ROM 22 (1998)(EMAP Images)(GB)[!][issue 1998-05].iso / PowerPC / Archivers / ZLib / inflate.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-20  |  8.5 KB  |  333 lines

  1. /* inflate.c -- zlib interface to inflate modules
  2.  * Copyright (C) 1995-1996 Mark Adler
  3.  * For conditions of distribution and use, see copyright notice in zlib.h 
  4.  */
  5.  
  6. #include "zutil.h"
  7. #include "infblock.h"
  8.  
  9. struct inflate_blocks_state {int dummy;}; /* for buggy compilers */
  10.  
  11. /* inflate private state */
  12. struct internal_state {
  13.  
  14.   /* mode */
  15.   enum {
  16.       METHOD,   /* waiting for method byte */
  17.       FLAG,     /* waiting for flag byte */
  18.       DICT4,    /* four dictionary check bytes to go */
  19.       DICT3,    /* three dictionary check bytes to go */
  20.       DICT2,    /* two dictionary check bytes to go */
  21.       DICT1,    /* one dictionary check byte to go */
  22.       DICT0,    /* waiting for inflateSetDictionary */
  23.       BLOCKS,   /* decompressing blocks */
  24.       CHECK4,   /* four check bytes to go */
  25.       CHECK3,   /* three check bytes to go */
  26.       CHECK2,   /* two check bytes to go */
  27.       CHECK1,   /* one check byte to go */
  28.       DONE,     /* finished check, done */
  29.       BAD}      /* got an error--stay here */
  30.     mode;               /* current inflate mode */
  31.  
  32.   /* mode dependent information */
  33.   union {
  34.     uInt method;        /* if FLAGS, method byte */
  35.     struct {
  36.       uLong was;                /* computed check value */
  37.       uLong need;               /* stream check value */
  38.     } check;            /* if CHECK, check values to compare */
  39.     uInt marker;        /* if BAD, inflateSync's marker bytes count */
  40.   } sub;        /* submode */
  41.  
  42.   /* mode independent information */
  43.   int  nowrap;          /* flag for no wrapper */
  44.   uInt wbits;           /* log2(window size)  (8..15, defaults to 15) */
  45.   inflate_blocks_statef 
  46.     *blocks;            /* current inflate_blocks state */
  47.  
  48. };
  49.  
  50.  
  51. int inflateReset(z_streamp z)
  52. {
  53.   uLong c;
  54.  
  55.   if (z == Z_NULL || z->state == Z_NULL)
  56.     return Z_STREAM_ERROR;
  57.   z->total_in = z->total_out = 0;
  58.   z->msg = Z_NULL;
  59.   z->state->mode = z->state->nowrap ? BLOCKS : METHOD;
  60.   inflate_blocks_reset(z->state->blocks, z, &c);
  61.   Trace((stderr, "inflate: reset\n"));
  62.   return Z_OK;
  63. }
  64.  
  65.  
  66. int inflateEnd(z_streamp z)
  67. {
  68.   uLong c;
  69.  
  70.   if (z == Z_NULL || z->state == Z_NULL || z->zfree == Z_NULL)
  71.     return Z_STREAM_ERROR;
  72.   if (z->state->blocks != Z_NULL)
  73.     inflate_blocks_free(z->state->blocks, z, &c);
  74.   ZFREE(z, z->state);
  75.   z->state = Z_NULL;
  76.   Trace((stderr, "inflate: end\n"));
  77.   return Z_OK;
  78. }
  79.  
  80.  
  81. int inflateInit2_(z_streamp z, int w, const char *version, int stream_size)
  82. {
  83.   if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
  84.       stream_size != sizeof(z_stream))
  85.       return Z_VERSION_ERROR;
  86.  
  87.   /* initialize state */
  88.   if (z == Z_NULL)
  89.     return Z_STREAM_ERROR;
  90.   z->msg = Z_NULL;
  91.   if (z->zalloc == Z_NULL)
  92.   {
  93.     z->zalloc = zcalloc;
  94.     z->opaque = (voidpf)0;
  95.   }
  96.   if (z->zfree == Z_NULL) z->zfree = zcfree;
  97.   if ((z->state = (struct internal_state FAR *)
  98.        ZALLOC(z,1,sizeof(struct internal_state))) == Z_NULL)
  99.     return Z_MEM_ERROR;
  100.   z->state->blocks = Z_NULL;
  101.  
  102.   /* handle undocumented nowrap option (no zlib header or check) */
  103.   z->state->nowrap = 0;
  104.   if (w < 0)
  105.   {
  106.     w = - w;
  107.     z->state->nowrap = 1;
  108.   }
  109.  
  110.   /* set window size */
  111.   if (w < 8 || w > 15)
  112.   {
  113.     inflateEnd(z);
  114.     return Z_STREAM_ERROR;
  115.   }
  116.   z->state->wbits = (uInt)w;
  117.  
  118.   /* create inflate_blocks state */
  119.   if ((z->state->blocks =
  120.       inflate_blocks_new(z,
  121.       (check_func)(z->state->nowrap ? Z_NULL : (uLong) adler32),
  122.       (uInt)1 << w))
  123.       == Z_NULL)
  124.   {
  125.     inflateEnd(z);
  126.     return Z_MEM_ERROR;
  127.   }
  128.   Trace((stderr, "inflate: allocated\n"));
  129.  
  130.   /* reset state */
  131.   inflateReset(z);
  132.   return Z_OK;
  133. }
  134.  
  135.  
  136. int inflateInit_(z_streamp z, const char *version, int stream_size)
  137. {
  138.   return inflateInit2_(z, DEF_WBITS, version, stream_size);
  139. }
  140.  
  141.  
  142. #define NEEDBYTE {if(z->avail_in==0)return r;r=Z_OK;}
  143. #define NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++)
  144.  
  145. int inflate(z_streamp z, int f)
  146. {
  147.   int r;
  148.   uInt b;
  149.  
  150.   if (z == Z_NULL || z->state == Z_NULL || z->next_in == Z_NULL || f < 0)
  151.     return Z_STREAM_ERROR;
  152.   r = Z_BUF_ERROR;
  153.   while (1) switch (z->state->mode)
  154.   {
  155.     case METHOD:
  156.       NEEDBYTE
  157.       if (((z->state->sub.method = NEXTBYTE) & 0xf) != Z_DEFLATED)
  158.       {
  159.         z->state->mode = BAD;
  160.         z->msg = (char*)"unknown compression method";
  161.         z->state->sub.marker = 5;       /* can't try inflateSync */
  162.         break;
  163.       }
  164.       if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
  165.       {
  166.         z->state->mode = BAD;
  167.         z->msg = (char*)"invalid window size";
  168.         z->state->sub.marker = 5;       /* can't try inflateSync */
  169.         break;
  170.       }
  171.       z->state->mode = FLAG;
  172.     case FLAG:
  173.       NEEDBYTE
  174.       b = NEXTBYTE;
  175.       if (((z->state->sub.method << 8) + b) % 31)
  176.       {
  177.         z->state->mode = BAD;
  178.         z->msg = (char*)"incorrect header check";
  179.         z->state->sub.marker = 5;       /* can't try inflateSync */
  180.         break;
  181.       }
  182.       Trace((stderr, "inflate: zlib header ok\n"));
  183.       if (!(b & PRESET_DICT))
  184.       {
  185.         z->state->mode = BLOCKS;
  186.         break;
  187.       }
  188.       z->state->mode = DICT4;
  189.     case DICT4:
  190.       NEEDBYTE
  191.       z->state->sub.check.need = (uLong)NEXTBYTE << 24;
  192.       z->state->mode = DICT3;
  193.     case DICT3:
  194.       NEEDBYTE
  195.       z->state->sub.check.need += (uLong)NEXTBYTE << 16;
  196.       z->state->mode = DICT2;
  197.     case DICT2:
  198.       NEEDBYTE
  199.       z->state->sub.check.need += (uLong)NEXTBYTE << 8;
  200.       z->state->mode = DICT1;
  201.     case DICT1:
  202.       NEEDBYTE
  203.       z->state->sub.check.need += (uLong)NEXTBYTE;
  204.       z->adler = z->state->sub.check.need;
  205.       z->state->mode = DICT0;
  206.       return Z_NEED_DICT;
  207.     case DICT0:
  208.       z->state->mode = BAD;
  209.       z->msg = (char*)"need dictionary";
  210.       z->state->sub.marker = 0;       /* can try inflateSync */
  211.       return Z_STREAM_ERROR;
  212.     case BLOCKS:
  213.       r = inflate_blocks(z->state->blocks, z, r);
  214.       if (r == Z_DATA_ERROR)
  215.       {
  216.         z->state->mode = BAD;
  217.         z->state->sub.marker = 0;       /* can try inflateSync */
  218.         break;
  219.       }
  220.       if (r != Z_STREAM_END)
  221.         return r;
  222.       r = Z_OK;
  223.       inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was);
  224.       if (z->state->nowrap)
  225.       {
  226.         z->state->mode = DONE;
  227.         break;
  228.       }
  229.       z->state->mode = CHECK4;
  230.     case CHECK4:
  231.       NEEDBYTE
  232.       z->state->sub.check.need = (uLong)NEXTBYTE << 24;
  233.       z->state->mode = CHECK3;
  234.     case CHECK3:
  235.       NEEDBYTE
  236.       z->state->sub.check.need += (uLong)NEXTBYTE << 16;
  237.       z->state->mode = CHECK2;
  238.     case CHECK2:
  239.       NEEDBYTE
  240.       z->state->sub.check.need += (uLong)NEXTBYTE << 8;
  241.       z->state->mode = CHECK1;
  242.     case CHECK1:
  243.       NEEDBYTE
  244.       z->state->sub.check.need += (uLong)NEXTBYTE;
  245.  
  246.       if (z->state->sub.check.was != z->state->sub.check.need)
  247.       {
  248.         z->state->mode = BAD;
  249.         z->msg = (char*)"incorrect data check";
  250.         z->state->sub.marker = 5;       /* can't try inflateSync */
  251.         break;
  252.       }
  253.       Trace((stderr, "inflate: zlib check ok\n"));
  254.       z->state->mode = DONE;
  255.     case DONE:
  256.       return Z_STREAM_END;
  257.     case BAD:
  258.       return Z_DATA_ERROR;
  259.     default:
  260.       return Z_STREAM_ERROR;
  261.   }
  262. }
  263.  
  264.  
  265. int inflateSetDictionary(z_streamp z, const Bytef *dictionary, uInt dictLength)
  266. {
  267.   uInt length = dictLength;
  268.  
  269.   if (z == Z_NULL || z->state == Z_NULL || z->state->mode != DICT0)
  270.     return Z_STREAM_ERROR;
  271.  
  272.   if (adler32(1L, dictionary, dictLength) != z->adler) return Z_DATA_ERROR;
  273.   z->adler = 1L;
  274.  
  275.   if (length >= ((uInt)1<<z->state->wbits))
  276.   {
  277.     length = (1<<z->state->wbits)-1;
  278.     dictionary += dictLength - length;
  279.   }
  280.   inflate_set_dictionary(z->state->blocks, dictionary, length);
  281.   z->state->mode = BLOCKS;
  282.   return Z_OK;
  283. }
  284.  
  285.  
  286. int inflateSync(z_streamp z)
  287. {
  288.   uInt n;       /* number of bytes to look at */
  289.   Bytef *p;     /* pointer to bytes */
  290.   uInt m;       /* number of marker bytes found in a row */
  291.   uLong r, w;   /* temporaries to save total_in and total_out */
  292.  
  293.   /* set up */
  294.   if (z == Z_NULL || z->state == Z_NULL)
  295.     return Z_STREAM_ERROR;
  296.   if (z->state->mode != BAD)
  297.   {
  298.     z->state->mode = BAD;
  299.     z->state->sub.marker = 0;
  300.   }
  301.   if ((n = z->avail_in) == 0)
  302.     return Z_BUF_ERROR;
  303.   p = z->next_in;
  304.   m = z->state->sub.marker;
  305.  
  306.   /* search */
  307.   while (n && m < 4)
  308.   {
  309.     if (*p == (Byte)(m < 2 ? 0 : 0xff))
  310.       m++;
  311.     else if (*p)
  312.       m = 0;
  313.     else
  314.       m = 4 - m;
  315.     p++, n--;
  316.   }
  317.  
  318.   /* restore */
  319.   z->total_in += p - z->next_in;
  320.   z->next_in = p;
  321.   z->avail_in = n;
  322.   z->state->sub.marker = m;
  323.  
  324.   /* return no joy or set up to restart on a new block */
  325.   if (m != 4)
  326.     return Z_DATA_ERROR;
  327.   r = z->total_in;  w = z->total_out;
  328.   inflateReset(z);
  329.   z->total_in = r;  z->total_out = w;
  330.   z->state->mode = BLOCKS;
  331.   return Z_OK;
  332. }
  333.